package com.tronsis.distribution.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.PayeeMethodBean;
import com.tronsis.distribution.biz.factory.CommonFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 收款方式
 */
public class CollectionWayActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_collection_way)
    RecyclerView mRv;


    private CommonAdapter<PayeeMethodBean> mAdapter;
    private List<PayeeMethodBean> mPayeeList = new ArrayList<>();
    private String mSelectPayeeId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection_way);
        setTitle(true, getString(R.string.collection_way));

        boolean isCompany = getIntent().getBooleanExtra("isCompany", false);
        mSelectPayeeId = getIntent().getStringExtra("PayeeId");

        initRv();

        httpListPayee(isCompany);
    }

    private void initRv() {
        mAdapter = new CommonAdapter<PayeeMethodBean>(mContext, mPayeeList, R.layout.item_collection_way) {
            @Override
            protected void convert(ViewHolder holder, PayeeMethodBean bean, int position) {
                if (StringUtil.isEquals(mSelectPayeeId, bean.getId())) {
                    holder.setVisible(R.id.iv_check, true);
                } else {
                    holder.setVisible(R.id.iv_check, false);
                }
                holder.setText(R.id.tv_collection_way, bean.getName() + "：" + bean.getBank() + "   " + bean.getAccount());
            }
        };

        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Intent intent = new Intent();
                intent.putExtra("PayeeMethodBean", mPayeeList.get(position));
                setResult(Activity.RESULT_OK, intent);
                finish();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRv.addItemDecoration(decoration);
        mRv.setLayoutManager(new LinearLayoutManager(mContext));
        mRv.setAdapter(mAdapter);
    }


    private void httpListPayee(boolean isCompany) {
        UICallBack callBack = new UICallBack() {
            @Override
            public void onStart() {
                showLoadingDialog();
            }

            @Override
            public void onRefresh(Object data) {
                dismissLoadingDialog();
                mPayeeList.clear();
                List<PayeeMethodBean> list = (List<PayeeMethodBean>) data;
                mPayeeList.addAll(list);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int status) {
                dismissLoadingDialog();

            }
        };


        if (isCompany) {
            try {
                CommonFactory.createBiz().getCompanyPayeeList(this, callBack);
            } catch (Exception e) {
                dismissLoadingDialog();
                e.printStackTrace();
            }
        } else {
            try {
                CommonFactory.createBiz().getLeaderPayeeList(this, callBack);
            } catch (Exception e) {
                dismissLoadingDialog();
                e.printStackTrace();
            }
        }


    }


}
